<template>
  <div
    class="mask"
    v-if="showCart && calculations.total > 0"
    @click="handleCartShowChange"
  ></div>
  <div class="cart">
    <div class="product" v-if="showCart && calculations.total > 0">
      <div class="product__header">
        <div class="product__header__all" @click="setCartItemChecked">
          <span
            class="product__header__icon iconfont"
            v-html="calculations.allChecked ? '&#xe66c;' : '&#xe7ae;'"
          ></span>
          全选
        </div>
        <div class="product__header__clear">
          <span @click.stop="clearCartProducts(shopId)"> 清空购物车 </span>
        </div>
      </div>
      <template v-for="item in productList" :key="item._id">
        <div class="product__item">
          <div
            class="product__item__checked iconfont"
            v-html="item.check ? '&#xe66c;' : '&#xe7ae;'"
            @click.stop="changeCartItemCheck(shopId, item._id)"
          />
          <img class="product__item__img" :src="item.imgUrl" alt="" />
          <div class="product__item__detail">
            <h4 class="product__item__title">
              {{ item.name }}
            </h4>
            <p class="product__item__price">
              <span class="product__item__yen">&yen;</span>{{ item.price }}
              <span class="product__item__origin"
                >&yen;{{ item.oldPrice }}</span
              >
            </p>
          </div>
          <div class="product__number">
            <span
              class="product__number__minus"
              @click="
                () => {
                  changeCartItemInfo(shopId, item._id, item, -1);
                }
              "
            >
              <span class="move">-</span>
            </span>
            {{ item.count || 0 }}
            <span
              class="product__number__plus"
              @click="
                () => {
                  changeCartItemInfo(shopId, item._id, item, 1);
                }
              "
              >+</span
            >
          </div>
        </div>
      </template>
    </div>
    <div class="check">
      <div class="check__icon">
        <img
          src="http://www.dell-lee.com/imgs/vue3/basket.png"
          class="check__icon__img"
          @click="handleCartShowChange"
        />
        <div class="check__icon__tag">{{ calculations.total }}</div>
      </div>
      <div class="check__info">
        总计：<span class="check__info__price"
          >&yen;{{ calculations.price }}</span
        >
      </div>
      <div class="check__btn" v-show="calculations.total > 0">
        <router-link :to="`/orderConfirmation/${shopId}`"> 去结算 </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useStore } from 'vuex';
import { useRoute } from 'vue-router';
import { useCommonCartEffect } from '@/effects/cartEffects';

// 购物车数量 总价 相关逻辑
const useCartEffect = (shopId) => {
  const { changeCartItemInfo, productList, calculations } =
    useCommonCartEffect(shopId);

  const store = useStore();

  // 改变商品选中状态
  const changeCartItemCheck = (shopId, productId) => {
    store.commit('changeCartItemChecked', { shopId, productId });
  };
  // 清空购物车
  const clearCartProducts = (shopId) => {
    store.commit('clearCartProducts', { shopId });
  };
  // 全选
  const setCartItemChecked = () => {
    store.commit('setCartItemChecked', { shopId });
  };
  return {
    productList,
    changeCartItemInfo,
    changeCartItemCheck,
    clearCartProducts,
    setCartItemChecked,
    calculations,
  };
};

// 展示隐藏购物车逻辑
const toggleCartEffect = () => {
  const showCart = ref(false);
  const handleCartShowChange = () => {
    showCart.value = !showCart.value;
  };
  return { showCart, handleCartShowChange };
};

export default {
  name: 'Cart',
  setup() {
    const route = useRoute();
    const shopId = route.params.id;
    const { showCart, handleCartShowChange } = toggleCartEffect();
    const {
      productList,
      changeCartItemInfo,
      changeCartItemCheck,
      clearCartProducts,
      setCartItemChecked,
      calculations,
    } = useCartEffect(shopId);
    console.log('productList: ', productList);
    return {
      productList,
      changeCartItemInfo,
      shopId,
      changeCartItemCheck,
      clearCartProducts,
      setCartItemChecked,
      showCart,
      handleCartShowChange,
      calculations,
    };
  },
};
</script>

<style lang="scss" scoped>
@import '@/style/viriables.scss';
@import '@/style/mixins.scss';
.product {
  overflow-y: scroll;
  flex: 1;
  &__header {
    height: 0.52rem;
    line-height: 0.52rem;
    border-bottom: 0.01rem solid $content-bgColor;
    display: flex;
    font-size: 0.14rem;
    color: $content-fontcolor;
    &__all {
      width: 0.64rem;
      margin-left: 0.16rem;
    }
    &__clear {
      flex: 1;
      text-align: right;
      margin-right: 0.16rem;
    }
    &__icon {
      color: $btn-bgColor;
      font-size: 0.2rem;
      display: inline-block;
      vertical-align: top;
    }
  }
  &__item {
    display: flex;
    padding: 0.12rem 0;
    margin: 0 0.16rem;
    border-bottom: 0.01rem solid $content-bgColor;
    position: relative;
    &__checked {
      color: $btn-bgColor;
      font-size: 0.2rem;
      line-height: 0.5rem;
      margin-right: 0.2rem;
    }
    &__img {
      width: 0.46rem;
      height: 0.46rem;
      margin-right: 0.16rem;
    }
    &__detail {
      overflow: hidden;
    }
    &__title {
      margin: 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $content-fontcolor;
      @include ellipsis;
    }
    &__price {
      margin: 0.06rem 0 0 0;
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $hightlight-fontColor;
    }
    &__yen {
      font-size: 0.12rem;
    }
    &__origin {
      margin-left: 0.06rem;
      line-height: 0.2rem;
      font-size: 0.12rem;
      color: $light-fontColor;
      text-decoration: line-through;
    }
  }
  &__number {
    position: absolute;
    right: 0.18rem;
    bottom: 0.12rem;
    &__minus,
    &__plus {
      display: inline-block;
      width: 0.2rem;
      height: 0.2rem;
      border-radius: 50%;
      font-size: 0.2rem;
      text-align: center;
      line-height: 0.2rem;
    }
    &__minus {
      border: 0.01rem solid $medium-fontColor;
      color: $medium-fontColor;
      margin-right: 0.05rem;
    }
    &__plus {
      background: $btn-bgColor;
      color: $bgColor;
      margin-left: 0.05rem;
    }
  }
}
.mask {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1;
}
.cart {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  background-color: $bgColor;
}
.check {
  display: flex;
  height: 0.49rem;
  border-top: 0.01rem solid $content-bgColor;
  line-height: 0.49rem;
  &__icon {
    position: relative;
    width: 0.84rem;
    &__img {
      display: block;
      margin: 0.12rem auto;
      width: 0.28rem;
      height: 0.26rem;
    }
    &__tag {
      position: absolute;
      right: 0.2rem;
      top: 0.04rem;
      min-width: 0.2rem;
      height: 0.2rem;
      line-height: 0.2rem;
      background-color: $hightlight-fontColor;
      border-radius: 50%;
      font-size: 0.14rem;
      text-align: center;
      color: $bgColor;
      // transform: scale(0.5);
    }
  }
  &__info {
    flex: 1;
    color: $content-fontcolor;
    font-size: 0.12rem;
    &__price {
      line-height: 0.49rem;
      color: $hightlight-fontColor;
      font-size: 0.18rem;
    }
  }
  &__btn {
    width: 0.98rem;
    background-color: #4fb0f9;
    text-align: center;
    color: $bgColor;
    font-size: 0.14rem;
    a {
      color: white;
      text-decoration: none;
    }
  }
}
</style>